﻿using Microsoft.AspNetCore.Mvc;
using ZhiJiao.Models.Entities.Enums;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Service.Dto;

namespace ZHIJIAO.Controllers
{
    public class CourseController : Controller
    {
        private readonly BackJson _res;
        private readonly ICurrentUser _currentUser;
        private readonly ICourseService _courseService;


        public CourseController(BackJson res,
                                ICurrentUser currentUser,
                                ICourseService courseService)
        { 
            _res = res;
            _currentUser = currentUser;
            _courseService = courseService;
        }

        /// <summary>
        /// 列表页视图
        /// </summary>
        public IActionResult Index()
        {
            ViewBag.UserType = Convert.ToInt32(_currentUser.User.Type);
            if (_currentUser.User.Type == UserType.Instruction)
            {
                ViewBag.InsId = _currentUser.User.Instruction!.Id;
            }
            else
            {
                ViewBag.InsId = 0;
            }
            return View();
        }

        /// <summary>
        /// 新建、编辑视图
        /// </summary>
        public IActionResult Add()
        {
            ViewBag.ActionType = Request.Query["actionType"].ToString();
            ViewBag.Id = -1;
            if (ViewBag.ActionType == "edit")
            {
                ViewBag.Id = Convert.ToInt32(Request.Query["id"]);
            }
            ViewBag.UserType = Convert.ToInt32(_currentUser.User.Type);
            ViewBag.InsId = _currentUser.User.Instruction!.Id;
            ViewBag.HostWithScheme = $"{Request.Scheme}://{Request.Host.Value}";
            return View();
        }

        [HttpGet("api/course/get-course-list")]
        public async Task<IActionResult> GetCourseList(string? name,CourseType? courseType)
        {
            return Json(await _courseService.GetCourseList(name,courseType));
        }

        /// <summary>
        /// 获取新课程对象
        /// </summary>
        [HttpGet("api/course/new-course")]
        public async Task<IActionResult> NewCourse()
        {
            return Json(await _courseService.NewCourse());
        }

        /// <summary>
        /// 加载单个课程
        /// </summary>
        [HttpGet("api/course/get-course")]
        public async Task<IActionResult> GetCourse(long id)
        {
            return Json(await _courseService.GetCourse(id));
        }

        /// <summary>
        /// 删除课程
        /// </summary>
        [HttpGet("api/course/delete-course")]
        public async Task<IActionResult> DeleteCourse(long id)
        {
            return Json(await _courseService.DeleteCourse(id));
        }
    }
}
